package com.ruoyi.project.oms.paeaAuAuditGroup.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.project.oms.paeaAuAuditGroupUserRel.domain.PaeaAuAuditGroupUserRel;
import com.ruoyi.project.oms.paeaAuAuditGroup.domain.PaeaAuAuditGroup;
import com.ruoyi.project.oms.paeaAuAuditGroupDeptRel.domain.PaeaAuAuditGroupDeptRel;
import com.ruoyi.project.oms.paeaAuAuditGroupUserRel.service.IPaeaAuAuditGroupUserRelService;
import com.ruoyi.project.oms.paeaAuAuditGroupDeptRel.service.IPaeaAuAuditGroupDeptRelService;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.oms.paeaAuAuditGroup.service.IPaeaAuAuditGroupService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import java.util.Arrays;

import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 监控小组Controller
 *
 * @author ruoyi
 * @date 2020-03-31
 */
@Controller
@RequestMapping("/oms/paeaAuAuditGroup")
public class PaeaAuAuditGroupController extends BaseController
{
    private String prefix = "oms/paeaAuAuditGroup";

    @Autowired
    private IPaeaAuAuditGroupService paeaAuAuditGroupService;
    @Autowired
    private IPaeaAuAuditGroupUserRelService paeaAuAuditGroupUserRelService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IPaeaAuAuditGroupDeptRelService paeaAuAuditGroupDeptRelService;
    @Autowired
    private IDeptService deptService;


    @RequiresPermissions("oms:paeaAuAuditGroup:view")
    @GetMapping()
    public String paeaAuAuditGroup()
    {
        return prefix + "/paeaAuAuditGroup";
    }

    /**
     * 查询监控小组列表
     */
    @RequiresPermissions("oms:paeaAuAuditGroup:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PaeaAuAuditGroup paeaAuAuditGroup)
    {
        startPage();
        List<PaeaAuAuditGroup> list = paeaAuAuditGroupService.list(new QueryWrapper<>(paeaAuAuditGroup));
        return getDataTable(list);
    }

    /**
     * 导出监控小组列表
     */
    @RequiresPermissions("oms:paeaAuAuditGroup:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PaeaAuAuditGroup paeaAuAuditGroup)
    {
        List<PaeaAuAuditGroup> list = paeaAuAuditGroupService.list(new QueryWrapper<>(paeaAuAuditGroup));
        ExcelUtil<PaeaAuAuditGroup> util = new ExcelUtil<PaeaAuAuditGroup>(PaeaAuAuditGroup.class);
        return util.exportExcel(list, "监控小组信息");
    }

    /**
     * 新增监控小组
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存监控小组
     */
    @RequiresPermissions("oms:paeaAuAuditGroup:add")
    @Log(title = "监控小组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PaeaAuAuditGroup paeaAuAuditGroup)
    {
        return toAjax(paeaAuAuditGroupService.saveOrUpdate(paeaAuAuditGroup));
    }

    /**
     * 修改监控小组
     */
    @GetMapping("/edit/{groupId}")
    public String edit(@PathVariable("groupId") String groupId, ModelMap mmap)
    {
        PaeaAuAuditGroup paeaAuAuditGroup = paeaAuAuditGroupService.getById(groupId);
        mmap.put("paeaAuAuditGroup", paeaAuAuditGroup);
        return prefix + "/edit";
    }

    /**
     * 修改保存监控小组
     */
    @RequiresPermissions("oms:paeaAuAuditGroup:edit")
    @Log(title = "监控小组", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PaeaAuAuditGroup paeaAuAuditGroup)
    {
        return toAjax(paeaAuAuditGroupService.saveOrUpdate(paeaAuAuditGroup));
    }

    /**
     * 删除监控小组
     */
    @RequiresPermissions("oms:paeaAuAuditGroup:remove")
    @Log(title = "监控小组", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(paeaAuAuditGroupService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 校验代码
     */
    @PostMapping("/checkCodeUnique")
    @ResponseBody
    public String checkCodeUnique(PaeaAuAuditGroup paeaAuAuditGroup)
    {
        if(paeaAuAuditGroupService.getOne(new QueryWrapper<>(paeaAuAuditGroup))!=null){
            return Constants.NOT_UNIQUE;
        }
        return Constants.UNIQUE;
    }

    /**
     * 监控小组人员分配
     */
    @RequiresPermissions("oms:paeaAuAuditGroup:paeaAuAuditGroupUserRelConfig")
    @GetMapping("/paeaAuAuditGroupUserRelConfig/{groupId}")
    public String paeaAuAuditGroupUserRelConfig(@PathVariable("groupId") String groupId, ModelMap mmap)
    {
        //全部监控人员
        mmap.put("allUsers", userService.selectAllUsers());
        //已分配监控人员
        mmap.put("paeaAuAuditGroupUserRels", paeaAuAuditGroupUserRelService.selectPaeaAuAuditGroupUserRelListByGroupId(groupId));
        mmap.put("groupId",groupId);
        return prefix + "/paeaAuAuditGroupUserRelConfig";
    }

    /**
     * 保存监控小组人员分配
     */
    @RequiresPermissions("oms:paeaAuAuditGroup:paeaAuAuditGroupUserRelConfig")
    @Log(title = "监控小组人员分配", businessType = BusinessType.UPDATE)
    @PostMapping("/paeaAuAuditGroupUserRelConfigSave/{groupId}")
    @ResponseBody
    public AjaxResult paeaAuAuditGroupUserRelConfigSave(@PathVariable("groupId") String groupId,String ids)
    {
        return AjaxResult.success(paeaAuAuditGroupUserRelService.auditGroupAssignUserConfigSave(groupId,ids));
    }

    /**
     * 查看监控小组人员
     */
    @GetMapping("/paeaAuAuditGroupUserRelDetail/{groupId}")
    public String paeaAuAuditGroupUserRelDetail(@PathVariable("groupId") String groupId, ModelMap mmap)
    {
        List<PaeaAuAuditGroupUserRel> paeaAuAuditGroupUserRels = paeaAuAuditGroupUserRelService.selectPaeaAuAuditGroupUserRelListByGroupId(groupId);
        mmap.put("paeaAuAuditGroupUserRels", paeaAuAuditGroupUserRels);
        mmap.put("groupId",groupId);
        return prefix + "/paeaAuAuditGroupUserRelDetail";
    }


    /**
     * 监控小组机构分配
     */
    @RequiresPermissions("oms:paeaAuAuditGroup:paeaAuAuditGroupDeptRelConfig")
    @GetMapping("/paeaAuAuditGroupDeptRelConfig/{groupId}")
    public String paeaAuAuditGroupDeptRelConfig(@PathVariable("groupId") String groupId, ModelMap mmap)
    {
        //全部监控机构
        mmap.put("allDepts", deptService.selectAllDepts());
        //已分配监控机构
        mmap.put("paeaAuAuditGroupDeptRels", paeaAuAuditGroupDeptRelService.selectPaeaAuAuditGroupDeptRelListByGroupId(groupId));
        mmap.put("groupId",groupId);
        return prefix + "/paeaAuAuditGroupDeptRelConfig";
    }

    /**
     * 保存监控小组机构分配
     */
    @RequiresPermissions("oms:paeaAuAuditGroup:paeaAuAuditGroupDeptRelConfig")
    @Log(title = "监控小组机构分配", businessType = BusinessType.UPDATE)
    @PostMapping("/paeaAuAuditGroupDeptRelConfigSave/{groupId}")
    @ResponseBody
    public AjaxResult paeaAuAuditGroupDeptRelConfigSave(@PathVariable("groupId") String groupId,String ids)
    {
        return AjaxResult.success(paeaAuAuditGroupDeptRelService.auditGroupAssignDeptConfigSave(groupId,ids));
    }

    /**
     * 查看监控小组机构
     */
    @GetMapping("/paeaAuAuditGroupDeptRelDetail/{groupId}")
    public String paeaAuAuditGroupDeptRelDetail(@PathVariable("groupId") String groupId, ModelMap mmap)
    {
        List<PaeaAuAuditGroupDeptRel> paeaAuAuditGroupDeptRels = paeaAuAuditGroupDeptRelService.selectPaeaAuAuditGroupDeptRelListByGroupId(groupId);
        mmap.put("paeaAuAuditGroupDeptRels", paeaAuAuditGroupDeptRels);
        mmap.put("groupId",groupId);
        return prefix + "/paeaAuAuditGroupDeptRelDetail";
    }




}
